var vm = new Vue({
    el: "#base",
    data: {
        isshow: 0,    // 显示的索引
        timer: null,  // 计时器
        drawer: false,   // 评论区显示
        contents: [],    // 评论区列表
        article: {},
        content: {
            uid: sessionStorage.getItem("user_id"), // 用户id
            aid: "", // 文章id
            comment: "", // 评论内容
        },     // 评论发表
        isFlipped:false,    // 翻转
    },
    created() {
        this.get_article();
    },
    mounted() {

    },
    // 页面关闭执行
    beforeUnmount() {
        clearInterval(this.timer)
    },
    methods: {
        // 获取内容
        get_article() {
            this.article = JSON.parse(sessionStorage.getItem("xq_art"));
        },
        // 显示评论
        get_content() {
            const aid = this.content.aid
            const url = "/article/content/" + this.cstart + "/" + aid;
            axios({
                url: url,
                method: "get"
            }).then(
                (resp) => {
                    // 返回成功
                    if (resp.data.statu) {
                        // 如果是第1页
                        if (this.cstart === 1) {
                            this.cend = resp.data.pageNum;  // 评论页数
                            this.contents = resp.data.datas
                            this.$refs.pinlun.addEventListener("scroll", this.btl2)
                        } else {
                            this.contents = this.contents.concat(resp.data.datas)
                        }
                    }
                }
            )
        },
        // 显示时间
        showTime(time) {
            const t = time.split(",")
            return t[0] + "年" + t[1] + "月" + t[2] + "日" + " " + t[3] + ":" + t[4];
        },
        // 添加评论
        add_content() {
            const formData = new FormData();
            if (this.content.comment === "") {
                this.$message.warning("输入内容为空，发表失败!");
                return;
            }
            for (let key in this.content) {
                formData.set(key + "", this.content[key])
            }
            axios({
                url: "/article/content",
                method: "post",
                data: formData
            }).then(
                (resp) => {
                    if (resp.data.statu) {
                        this.$message.success("发表成功!")
                        this.content.comment = "";  // 清空发表框
                        this.cstart = 1;
                        this.get_content();
                    } else {
                        this.$message.error("后台故障，请联系工作人员!")
                    }
                }
            )
        },
        // 滚轮事件
        btl2(event) {
            const div = event.target;
            const leng = div.scrollHeight - div.scrollTop - div.clientHeight;
            // 如果小于346px,则给进度加一,然后发送请求
            if (this.cstart < this.cend) {
                if (leng < 346 && leng > 1) {
                    this.cstart++;
                    this.get_content()   // 获取数据
                }
            } else if (this.cstart === this.cend) {
                if (leng <= 1) {
                    this.cstart++;   //只显示一次
                    this.$message.success("已经到底了哦")
                }
            }

        },
        // 关闭评论区
        closeContent() {
            const div = this.$refs.pinlun;
            div.removeEventListener("scroll", this.btl2)   // 删除监听器
            this.drawer = false;
        },
        // 打开评论区
        openContent() {
            const aid = sessionStorage.getItem("aid");  // 获取文章id
            this.drawer = true; // 先打开评论区
            this.content.aid = aid; // 文章id
            this.content.uid = sessionStorage.getItem("user_id");   // 获取用户id
            this.cstart = 1;  //第一页
            this.get_content(); // 获取评论
        },
        // 返回首页
        to_home() {
            window.close()
        },
    }
})

var quill = new Quill(".art", {
    modules: {
        toolbar: null
    },
    theme: "snow",   // 主题
})

quill.enable(false)